<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Groups/classes/PhpFox_ServiceGallery.class.php, $Author:  $ */



/** Service for working with groups gallery
 * 
 * @package module.groups
 * @version $Id: PhpFox_ServiceGallery.class.php 398 2008-03-11 12:43:26Z phpfoxsv $
 *  Max Kovrigovich
 */
class PhpFox_Mod_Groups_ServiceGallery extends BaseService 
{
    /**
     * Retrive group pictures list
     *
     * @param int $iGroupId group id
     * @param int $iPage page number
     * @param int $iPageSize page size
     * @return array of total count and data
     */
    
    function getImages($iGroupId, $iPage = 0, $iPageSize = 0, $bRand = 0)
    {
        $iGroupId = intval($iGroupId);
        $oGroupGalleryItem = $this->_oModule->getItem('GroupGallery');
        $sSql = 'SELECT COUNT(id) AS cnt FROM '.App::getT('group_gallery').' WHERE `group_id` = "'.$iGroupId.'"';
		$aResult = $oGroupGalleryItem->oDb->getRow($sSql);
		$nTotalCnt = $aResult['cnt'];

		$sSql = 'SELECT `id`, `default`, `user`
				 FROM '.App::getT('group_gallery').'
				 WHERE `group_id` = "'.$iGroupId.'"
				 ORDER BY '.(($bRand == 0)?'`id` DESC':'RAND()').
                 (($iPage > 0)?(' LIMIT '.($iPage - 1)*$iPageSize.','.$iPageSize):'');
        $aResult = $oGroupGalleryItem->oDb->getRows($sSql);
		return array($nTotalCnt, $aResult);
		//	$total_members = mysql_num_rows($total_check);
    }

	/**
	 * Select prev, current and next pic from group gallery
	 *
	 * @param int $iGroupId
	 * @param int $iImageId
	 * @return unknown
	 */
    function getImagesPrevNext($iGroupId, $iImageId)
    {
        $iImageId = intval($iImageId);
        $iGroupId = intval($iGroupId);
        
        $oGroupGalleryItem = $this->_oModule->getItem('GroupGallery');
        $sSql = 'SELECT id, title, user FROM '.App::getT('group_gallery').' WHERE `group_id`='.$iGroupId.' AND `id`='.$iImageId;
        $aImage = $oGroupGalleryItem->oDb->getRow($sSql);

        $aImageNext = $aImagePrev = array();
        if(count($aImage))
        {            
            $sSql = 'SELECT id, title FROM '.App::getT('group_gallery').' 
					 WHERE `group_id`='.$iGroupId.' AND `id`<'.$iImageId.' 
					 ORDER BY `id` DESC LIMIT 1';
            $aImageNext= $oGroupGalleryItem->oDb->getRow($sSql);
            $sSql = 'SELECT id, title FROM '.App::getT('group_gallery').' 
					 WHERE `group_id`='.$iGroupId.' AND `id`>'.$iImageId.' 
					 ORDER BY `id` ASC LIMIT 1';
            $aImagePrev = $oGroupGalleryItem->oDb->getRow($sSql);
        }
        return array($aImagePrev, $aImage, $aImageNext);
    }
    
    /**
     * Set dafault foto flag
     *
     * @param int $iGroupId
     * @param int $iImageId
     */
    function setDefaultImage($iGroupId, $iImageId)
    {
        $iImageId = intval($iImageId);
        $iGroupId = intval($iGroupId);
        $oGroupGalleryItem = $this->_oModule->getItem('GroupGallery');
        $sSql = 'SELECT COUNT(id) AS cnt FROM '.App::getT('group_gallery').' WHERE `group_id`='.$iGroupId.' AND `id`="'.$iImageId.'"';
        $aResult = $oGroupGalleryItem->oDb->getRow($sSql);
        if($aResult['cnt'])
        {
			$sSql = 'UPDATE '.App::getT('group_gallery').' SET  `default` = "" WHERE `group_id`='.$iGroupId;
			$oGroupGalleryItem->oDb->query($sSql);
			$sSql = 'UPDATE '.App::getT('group_gallery').' SET  `default` = "1" WHERE `group_id`='.$iGroupId.' AND `id`="'.$iImageId.'"';
			$oGroupGalleryItem->oDb->query($sSql);
        }
    }
    
    /**
     * Delete gallery item
     *
     * @param int $iImageId
     */
    function deleteItem($iImageId)
    {
        $iImageId = intval($iImageId);
        $oGroupGalleryItem = $this->_oModule->getItem('GroupGallery');
        $sImagePath = App::getParam('groups.pic.path').$iImageId.'.jpg';
        $sImageThumbPath = App::getParam('groups.pic.path').'thumb/'.$iImageId.'.jpg';
        if (file_exists($sImagePath))
            @unlink($sImagePath);
        if (file_exists($sImageThumbPath))
            @unlink($sImageThumbPath);
        $sSql = 'DELETE FROM '.App::getT('group_gallery').' WHERE id="'.$iImageId.'"';
        $oGroupGalleryItem->oDb->query($sSql);
    }
    
    /**
     * Check image E group
     *
     * @param int $iGroupId
     * @param int $iImageId
     * @return int
     */
    function isGroupImage($iGroupId, $iImageId)
    {
        $iImageId = intval($iImageId);
        $iGroupId = intval($iGroupId);
        $oGroupGalleryItem = $this->_oModule->getItem('GroupGallery');
        $sSql = 'SELECT COUNT(id) AS cnt FROM `'.App::getT('group_gallery').'` WHERE `group_id`= '.$iGroupId.' AND `id`='.$iImageId;
        $aResult = $oGroupGalleryItem->oDb->getRow($sSql);
        return $aResult['cnt'];
    }
    
    function isImageOwner($iImageId, $sUser)
    {
        $oGroupGalleryItem = $this->_oModule->getItem('GroupGallery');
        $sSql = 'SELECT COUNT(id) AS cnt FROM `'.App::getT('group_gallery').'` WHERE `id`= '.$iImageId.' AND `user`="'.$oGroupGalleryItem->oDb->escape($sUser).'"';
        $aResult = $oGroupGalleryItem->oDb->getRow($sSql);
        return $aResult['cnt'];
    }
 }
?>